home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Essential Home & Business Collection
/
The Essential Home & Business Collection.iso
/
27
/
3
/
5
/
HP22D5.ZIP
/
EXTERN
/
GLOBALS.C
< prev
next >
Wrap
Text File
|
1991-04-16
|
4KB
|
179 lines
#include "extern.h" /* Extensions need these! */
#include "io.h"
#define ID 0x1234 /* unique ID for this file type */
/*
** This routine saves all of the global variables to a file.
**
** To call this handler from HyperPAD:
**
** readGlobals;
*/
int ReadGlobals(int NumArgs,HANDLE hFileName)
{
HANDLE hName,hValue;
int handle;
WORD w;
if (NumArgs != 1) return(STOP);
/* open the file, check to see if it opened successfully */
handle = open(deref(hFileName),READ);
if (handle == -1) return(STOP);
/* read the ID and check if it is valid */
read(handle,&w,sizeof(w));
if (w != ID) goto closeit;
/* read the size of the first name */
read(handle,&w,sizeof(w));
while (w != 0xffff) {
/* allocate and read in the name of the global variable */
hName = NewHandle(w + 1);
read(handle,deref(hName),w);
deref(hName)[w] = '\0';
/* read in the size of the value */
read(handle,&w,sizeof(w));
/* allocate and read in the value */
hValue = NewHandle(w + 1);
read(handle,deref(hValue),w);
deref(hValue)[w] = '\0';
/* set the global in HyperPAD */
SetGlobal(hName,hValue);
/* free the name (don't deallocate the value) */
FreeHandle(hName);
/* read in the size of the next global's name (0xFFFF == EOF) */
read(handle,&w,sizeof(w));
}
closeit:
close(handle);
return(STOP);
}
/*
** This routine saves all of the global variables to a file.
**
** To call this handler from HyperPAD:
**
** saveGlobals "globals.dat";
*/
int SaveGlobals(int NumArgs,HANDLE hFileName)
{
HANDLE hdl;
PTR p,n;
int handle;
HANDLE hName,hValue;
WORD w;
if (NumArgs != 1) return(STOP);
/* create the file, check to see if the open was successful */
handle = create(deref(hFileName));
if (handle < 0) return(STOP);
/* get a comma-delimited list of global variable names */
hdl = GetGlobals();
/* allocate space for a single name */
hName = NewHandle(50);
p = LockHandle(hdl);
/* write the unique ID to the file */
w = ID;
write(handle,&w,sizeof(w));
while (TRUE) {
// figure out the end of the name
n = deref(hName);
while (*p && *p != ',') *n++ = *p++;
*n = '\0';
/* write the length of the name to the file */
w = strlen(deref(hName));
write(handle,&w,sizeof(w));
/* write the name to the file */
write(handle,deref(hName),w);
/* get the global's value */
hValue = GetGlobal(hName);
/* write the value's length to the file */
w = strlen(deref(hValue));
write(handle,&w,sizeof(w));
/* write the value to the file */
write(handle,deref(hValue),w);
/* free the value (we are done with it) */
FreeHandle(hValue);
/* end of global name list? */
if (*p) p++;
else break;
}
/* write the end-of-file marking */
w = 0xffff;
write(handle,&w,sizeof(w));
close(handle);
/* free the global variable name list and the temporary name holder */
FreeHandle(hdl);
FreeHandle(hName);
return(STOP);
}
/*
** This routine returns a comma-delimited list of global variable names to
** HyperPAD.
**
** To call this function:
**
** put the globals into page field 1;
**
** if "firstLine" is in globals() then beep;
*/
GlobalNames(int NumArgs)
{
ReturnValue(GetGlobals());
return(STOP);
}
POOL pascal Pool[] = {
{ "GetGlobals",
ReadGlobals,
0,
HANDLER},
{ "PutGlobals",
SaveGlobals,
0,
HANDLER},
{ "Globals",
GlobalNames,
0,
FUNCTION},
{ NULL,
NULL,
0,
0} };